package edu.scau.mis.pos.service.impl;


import edu.scau.mis.pos.dto.ProductSkusDTO;
import edu.scau.mis.pos.mapper.StoreMapper;
import edu.scau.mis.pos.service.IStoreService;
import edu.scau.mis.pos.vo.ProductSkusVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StoreServiceImpl implements IStoreService {

    @Autowired
    private StoreMapper storeMapper;

    @Override
    public List<ProductSkusDTO> selectProductAndSkuDetails() {
        return storeMapper.selectProductAndSkuDetails();
    }

    @Override
    public List<ProductSkusDTO> selectProductAndSkuDetailsByCondition(ProductSkusVo productSkusVo) {
        return storeMapper.selectProductAndSkuDetailsByCondition(productSkusVo);
    }
    @Override
    public int deleteProductAndSkuDetailsByCode(String skuCode){
        return storeMapper.deleteProductAndSkuDetailsByCode(skuCode);
    }
    @Override
    public int deleteProductAndSkuDetailsByCodes(String[] skuCode){
        return storeMapper.deleteProductAndSkuDetailsByCodes(skuCode);
    }

    @Override
    public int toggleIsAvailableBySkuCode(String skuCode) {
        int status = storeMapper.selectIsAvailableBySkuCode(skuCode);
        return storeMapper.toggleIsAvailableBySkuCode(skuCode,status);
    }

    @Override
    public int updateStockBySkuCode(ProductSkusDTO productSkusDTO) {
        int i = storeMapper.updateStockBySkuCode(productSkusDTO);
        //查询商品总库存，更新库存量insertProductStockQuantityBySn
        int stockQuantity = storeMapper.selectTotalStockQuantityBySn(productSkusDTO.getProductSn());
        storeMapper.insertProductStockQuantityBySn(productSkusDTO.getProductSn(), stockQuantity);

        return i;
    }

    @Override
    public int insertStock(ProductSkusDTO productSkusDTO) {

        //生成sku_code
//        String sku_code = productSkusDTO.getProductSn() + "_" + productSkusDTO.getColor()  + "_" + productSkusDTO.getSize();
//        productSkusDTO.setSkuCode(sku_code);
        //更新商品页的库存(商品总库存+插入的库存量)
        int addQuantity = productSkusDTO.getStockQuantity();
        int stockQuantity = storeMapper.selectTotalStockQuantityBySn(productSkusDTO.getProductSn())+ addQuantity;
        System.out.println(productSkusDTO);
        storeMapper.insertProductStockQuantityBySn(productSkusDTO.getProductSn(), stockQuantity);

        return storeMapper.insertStock(productSkusDTO);
    }

    @Override
    public boolean hasAvailableSku(String productSn) {
        return storeMapper.countAvailableSkusByProductSn(productSn) > 0;
    }




}
